<template>
	<view class="ep-picker-box">
		<!-- 蒙版区域 开始 -->
		<view class="ep-mask-box" v-show="show" @tap="show=false"></view>
		<!-- 蒙版区域 开始 -->

		<!-- 输入框区域 开始 -->
		<view class="ep-input-box" @tap="openOptions" :class="{'disabled':disabled}">
			<image v-if="IndexIamge" class="ep-input-ZXImg" src="../../static/img/hosecode.svg"></image>
			<view v-else></view>
			<view class="ep-input-title">{{showLabel}}</view>
			<text v-if="!show" class="iconfont icon-zuojiantou2 "></text>
			<text v-else class="iconfont icon-zuojiantou2 lll"></text>
		</view>
		<!-- 输入框区域 结束 -->

		<!-- 弹出的下拉区域 开始 -->
		<view v-show="show" class="ep-picker-content-wrap">
			<view class="ep-picker-content">
				<!-- 展示下拉项列表 开始 -->
				<view v-for="item in options" :key="item[value_key]"
					:class="{'disabled':item.disabled,'active':value==item[value_key]}" class="option-item"
					@tap="itemClick(item)">{{item[label_key]}}</view>
				<!-- 展示下拉项列表 结束 -->

				<!-- 下拉列表为空 开始 -->
				<view v-if="options.length==0" class="option-no-data">无数据</view>
				<!-- 下拉列表为空 结束 -->
			</view>
			<text class="triangle"></text>
		</view>
		<!-- 弹出的下拉区域 结束 -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				left: 0,

			}
		},
		props: {
			value: {
				type: [String, Number],
				default: ""
			},
			options: {
				type: Array,
				default: function() {
					return []
				}
			},
			value_key: {
				type: String,
				default: "value"
			},
			label_key: {
				type: String,
				default: "label"
			},
			disabled: {
				type: Boolean,
				default: false
			},
			IndexIamge: {
				type: Boolean,
				default: false
			}
		},
		model: {
			prop: 'value',
			event: "valChange"
		},

		methods: {
			//点击选中选项
			itemClick(item) {
				if (item.disabled) return

				//关闭
				this.show = false
				//修改v-model的值
				this.$emit('valChange', item[this.value_key])
				//将事件通知父组件
				this.$emit('change', item[this.value_key])
			},
			//展开选项
			openOptions() {
				if (!this.disabled) {
					this.show = true
				}
			}
		},
		computed: {
			showLabel() {
				var index = this.options.findIndex(item => {
					return item[this.value_key] == this.value
				})
				if (index != -1) {
					return this.options[index][this.label_key]
				} else if (!this.value) {
					return "请选择"
				} else {
					return this.value
				}
			}
		}
	}
</script>

<style scoped>
	/* 引入字体图标 */

	/* @import url("../../assets/iconfont/iconfont.css"); */
	.ep-picker-box {
		width: 210px;
		height: 35px;
		box-sizing: border-box;
		position: relative;
		font-size: 14px;
		color: #333;
		max-width: 300px;
		border: 1px solid #B3E7EA;
		border-radius: 2px;
	}

	.ep-mask-box {
		position: fixed;
		z-index: 999;
		top: 0;
		right: 0;
		left: 0;
		bottom: 0;
		background: none;
	}

	.ep-input-box {
		/* border: 1px solid rgb(229, 229, 229); */
		border-radius: 4px;
		position: relative;
		cursor: pointer;
		background: #FFFFFF;
		font-size: 16px;
		font-family: SourceHanSansCN-Medium, SourceHanSansCN;
		font-weight: 500;
		color: #333333;
		line-height: 24px;
		padding-left: 10px;
	}

	.ep-input-ZXImg {
		width: 60rpx;
		height: 60rpx;
	}

	.ep-input-title {
		display: flex;
		align-items: center;
		min-height: 33px;
		border: none;
	}

	/* 整个下拉组件禁用样式 */
	.ep-input-box.disabled {
		cursor: not-allowed;
		background-color: #f5f7fa;
		color: #999;
	}

	/* 展开收起箭头样式 */
	.ep-input-box .iconfont {
		position: absolute;
		top: 20%;
		right: 5px;
		font-size: 20px;
		/* transform: translateY(-50%); */
		transform: rotate(-90deg);
		color: #B8B8B8;
	}

	/* 下拉容器样式 外层 */
	.ep-picker-content-wrap {
		width: 100%;
		position: absolute;
		top: 40px;
		left: 0;
		z-index: 9999;
		padding-top: 6px;
	}

	/* 下拉容器样式 内层 */
	.ep-picker-content-wrap .ep-picker-content {
		background-color: #fff;
		padding: 3px 0;
		box-shadow: 0 0 20px 5px rgb(0 0 0 / 30%);
		border-radius: 5px;
		max-height: 181px;
		overflow: scroll;
		z-index: 9999;
	}

	/* 下拉项通用样式 */
	.ep-picker-content-wrap .ep-picker-content .option-item {
		padding: 8px 18px;
		height: 20px;
		cursor: pointer;
	}

	/* 无下拉项数据时样式 */
	.ep-picker-content-wrap .ep-picker-content .option-no-data {
		padding: 8px 18px;
		cursor: text;
		color: #999;
		text-align: center;
	}

	/* 鼠标移入下拉项样式 */
	.ep-picker-content-wrap .ep-picker-content .option-item:hover {
		background-color: #f5f7fa;
	}

	/* 已选中的下拉项样式 */
	.ep-picker-content-wrap .ep-picker-content .option-item.active {
		color: #007AFF;
	}

	/* 禁用的下拉项样式 */
	.ep-picker-content-wrap .ep-picker-content .option-item.disabled {
		color: #c0c4cc;
	}

	.ep-picker-content-wrap .ep-picker-content .option-item.disabled:hover {
		cursor: not-allowed;
	}

	/* 下拉容器指示箭头样式 */
/* 	.ep-picker-content-wrap .triangle {
		width: 0;
		height: 0;
		border-top: 6px solid rgba(0, 0, 0, 0);
		border-right: 6px solid rgba(0, 0, 0, 0);
		border-bottom: 6px solid #fff;
		border-left: 6px solid rgba(0, 0, 0, 0);
		position: absolute;
		top: -6px;
		left: 50%;
		transform: translateX(-50%);
		box-sizing: content-box;
	} */
	.ep-picker-content-wrap .triangle {
		top: -5px;
		left: 50%;
		margin-right: 3px;
		border-top-width: 0;
		border-bottom-color: #ebeef5;
		border-width: 6px;
		filter: drop-shadow(0 2px 12px rgba(0, 0, 0, .03));
		position: absolute;
		display: block;
		width: 0;
		height: 0;
		border-color: transparent;
		border-style: solid;
	}
	
	.triangle::after {
		content: " ";
		border-width: 6px;
		position: absolute;
		display: block;
		width: 0;
		height: 0;
		border-color: transparent;
		border-style: solid;
		top: -1px;
		/* margin-left: -78px; */
		border-top-width: 0;
		border-bottom-color: #fff;
	}

	::v-deep .icon-zuojiantou2:before {
		color: #80DBE0;
	}

	.lll {
		transform: rotate(90deg) !important;
	}
</style>
